// GetPropertyFunctionReader.cpp: implementation of the GetPropertyFunctionReader class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GetPropertyFunctionReader.h"
#include "GetPropertyFunction.h"
#include "part.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC_CREATE(GetPropertyFunctionReader, PartDBReader, FunctionObject::eGETPROPERTYFUNCTION);

GetPropertyFunctionReader::GetPropertyFunctionReader()
{

}

GetPropertyFunctionReader::~GetPropertyFunctionReader()
{

}

FunctionObject* GetPropertyFunctionReader::Reader(const _RecordsetPtr& rs)
{
	FunctionObject* pObject = PartDBReader::CreateObject(rs);
	ASSERT(pObject);

	ASSERT(pObject->IsKindOf(RUNTIME_CLASS_EX(GetPropertyFunction)));
	GetPropertyFunction* pGPFunction = static_cast<GetPropertyFunction*>(pObject);

	CString strTemp;
	strTemp = this->GetString(1, rs);
	INT nPrior = atoi(strTemp);
	
	strTemp = this->GetString(5, rs);
	INT nOrder = atoi(strTemp);

	pGPFunction->Prior((nPrior<<16)|(nOrder));
	
	strTemp = this->GetString(2, rs);
	ParserCondition(strTemp, pGPFunction);

	strTemp = this->GetString(3, rs);
	ParserTable(strTemp, pGPFunction);

	strTemp = this->GetString(4, rs);
	pGPFunction->Regular(strTemp);

	return pGPFunction;
}

void GetPropertyFunctionReader::ParserCondition(const CString& strCondition, GetPropertyFunction* pGPFunction)
{
	CString strTemp;
	INT nCount = 0;
	INT nNum = strCondition.GetLength();

	if(strCondition.IsEmpty())
		return;

	for(int i = 0; i < nNum; i++)
	{
		if(strCondition.GetAt(i) != _T(','))
		{
			strTemp += strCondition.GetAt(i);
		}
		else
		{
			ASSERT(&GetPart());
			
			Variable* pVar = GetPart().GetProperty(strTemp);
			ASSERT(pVar);
			pGPFunction->AddCondition(pVar);
			nCount = 0;
			strTemp.Empty();
		}
	}
	ASSERT(&GetPart());
	
	Variable* pVar = GetPart().GetProperty(strTemp);
	ASSERT(pVar);
	pGPFunction->AddCondition(pVar);
}

void GetPropertyFunctionReader::ParserTable(const CString& strTable, GetPropertyFunction* pGPFunction)
{
	CString strTemp;
	INT nNum = strTable.GetLength();

	for(int i = 0; i < nNum; i++)
	{
		if(strTable.GetAt(i) != _T(','))
		{
			strTemp += strTable.GetAt(i);
		}
		else
		{
			pGPFunction->AddTable(strTemp);
			strTemp.Empty();
		}
	}
	pGPFunction->AddTable(strTemp);
}